""" 
sw-520D 倾斜角传感器
接线:
    tilt 输入in --> GPIO26
    VCC --> 5V
    GND --> GND
"""

from machine import Pin
import time

TiltPin = 26  # 倾斜传感器Pin端口
LedPin = 15  # LED端口


def irq_call_back(tilt_pin):
    """通过中断读取输入硬件状态"""
    value = tilt_pin.value()
    if value == 0:
        led_pin.value(1)
        print("倾斜")
    else:
        led_pin.value(0)
        print("未倾斜, 平整")


# 程序入口
if __name__ == "__main__":
    led_pin = Pin(LedPin, Pin.OUT)  # 倾斜状态指示灯
    tilt_pin = Pin(TiltPin, Pin.IN, Pin.PULL_UP)  # 倾斜传感器输入端口
    tilt_pin.irq(trigger=Pin.IRQ_FALLING, handler=irq_call_back)
    while True:
        pass
